﻿Imports System.Text

Namespace DotNetNuke.Modules.ActiveForums.Controls
    Public Class CategoriesList
        Public Property PortalId() As Integer = -1
        Public Property ModuleId() As Integer = -1
        Public Property TabId() As Integer = -1
        Public Property ForumId() As Integer = -1
        Public Property ForumGroupId() As Integer = -1
        Public Property SelectedValues() As String = String.Empty
        Public Property Template() As String = String.Empty
        Public Property HeaderTemplate() As String = String.Empty
        Public Property FooterTemplate() As String = String.Empty
        Public Property SelectedCategory() As Integer = -1
        Public Property CSSClass() As String = "afn-category"
        Sub New(ByVal PortalId As Integer, ByVal ModuleId As Integer)
            Me.PortalId = PortalId
            Me.ModuleId = ModuleId
        End Sub
        Sub New(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal ForumGroupId As Integer)
            Me.PortalId = PortalId
            Me.ModuleId = ModuleId
            Me.ForumId = ForumId
            Me.ForumGroupId = ForumGroupId
        End Sub
        Public Function RenderView() As String
            Dim sb As New StringBuilder()
            Dim forumInfo As Forum = Nothing
            Dim groupPrefix As String = String.Empty
            Dim forumPrefix As String = String.Empty
            If ForumId > 0 Then
                Dim fc As New ForumController
                forumInfo = fc.GetForum(PortalId, ModuleId, ForumId)
                If Not forumInfo Is Nothing Then
                    groupPrefix = forumInfo.ForumGroup.PrefixURL
                    forumPrefix = forumInfo.PrefixURL
                End If

            End If
            Dim cUtils As New ControlUtils
            Using dr As IDataReader = DataProvider.Instance.Tags_List(PortalId, ModuleId, True, 0, 200, "ASC", "TagName", ForumId, ForumGroupId)
                dr.NextResult()
                While dr.Read
                    Dim tmp As String = Template
                    Dim categoryName As String = dr("TagName").ToString
                    tmp = tmp.Replace("[CATEGORYURL]", cUtils.BuildUrl(TabId, ModuleId, groupPrefix, forumPrefix, ForumGroupId, ForumId, -1, Integer.Parse(dr("TagId").ToString), Utilities.CleanName(categoryName), 1, -1))
                    tmp = tmp.Replace("[CATEGORYNAME]", categoryName)
                    If Integer.Parse(dr("TagId").ToString) = SelectedCategory Then
                        tmp = tmp.Replace("[CSSCLASS]", CSSClass & "-selected")
                    Else
                        tmp = tmp.Replace("[CSSCLASS]", CSSClass)
                    End If
                    sb.Append(tmp)
                End While
                dr.Close()
            End Using
            If sb.Length > 0 Then
                sb.Insert(0, HeaderTemplate)
                sb.Append(FooterTemplate)
            End If
            Return sb.ToString
        End Function
        Public Function RenderEdit() As String
            Dim sb As New StringBuilder()
            Dim sSelected As String = String.Empty
            Using dr As IDataReader = DataProvider.Instance.Tags_List(PortalId, ModuleId, True, 0, 200, "ASC", "TagName", ForumId, ForumGroupId)
                dr.NextResult()
                While dr.Read
                    sb.Append("<li>")
                    sb.Append("<input type=""checkbox""")
                    If IsSelected(dr("TagName").ToString) Then
                        sb.Append(" checked=""checked"" ")
                        sSelected &= dr("TagId").ToString & ";"
                    End If
                    sb.Append(" onclick=""amaf_catSelect(this);"" value=""" & dr("TagId").ToString & """ id=""amaf_cat_" & dr("TagId").ToString & """ />")
                    sb.Append(dr("TagName").ToString)
                    sb.Append("</li>")
                End While
                dr.Close()
            End Using
            If sb.Length > 0 Then
                sb.Insert(0, "<ul class=""af-catlist"">")
                sb.Append("</ul>")
                sb.Append("<input type=""hidden"" name=""amaf-catselect"" id=""amaf-catselect"" value=""" & sSelected & """ />")
            End If
            Return sb.ToString
        End Function
        Private Function IsSelected(ByVal TagName As String) As Boolean
            If String.IsNullOrEmpty(SelectedValues) Then
                Return False
            Else
                For Each s As String In SelectedValues.Split(CChar("|"))
                    If Not String.IsNullOrEmpty(s) Then
                        If s.ToLowerInvariant.Trim = TagName.ToLowerInvariant.Trim Then
                            Return True
                        End If
                    End If
                Next
            End If

        End Function

    End Class
End Namespace

